<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class hot {
	
	function __construct() {
		$this->defaultAction();
		exit();
	}

	function defaultAction(){		
		$tpl = new Template("movie/hot.htm" );
		$db  = getDB();
		$url = getUrl();

		if($url['params']['1'] == 'page'){
			$pagetemp = $url['params']['2'];		
		}
		
		$weektime = strtotime(date('Y-m-d',time()))  - 86400 * 14 ;
		
		//两周最多短评的电影
		$sql = "SELECT mid,count(mid) AS cnum FROM movie_comments WHERE addtime >= '$weektime' GROUP BY mid LIMIT 0,20";
		$db->query($sql);
		$comments = $db->fetchAll();
		//de($comments);
		
		//两周最多影评的电影
		$sql = "SELECT mid,count(mid) AS rnum FROM movie_review WHERE addtime >= '$weektime' GROUP BY mid LIMIT 0,20";
		$db->query($sql);
		$reviews = $db->fetchAll();
		//de($reviews);

		$movie_tmp = array_merge($comments,$reviews);
		//de($movie_tmp);
		foreach($movie_tmp as $k => $v){
			$movieid[$v['mid']]['mid'] = $v['mid'];
			if(!empty($v['cnum'])){
				$movieid[$v['mid']]['cnum'] = $v['cnum'];		
			}
			if(!empty($v['rnum'])){
				$movieid[$v['mid']]['rnum'] = $v['rnum'];
			}
		}
		//de($movieid);

		foreach($movieid as $k => $v){
			$movieid[$k]['num'] = $v['cnum'] + $v['rnum'];
			unset($movieid[$k]['rnum']);
			unset($movieid[$k]['cnum']);
		}
		//de($movieid);

		$temp = multi_array_sort($movieid, 'num',SORT_DESC);
		//de($temp);
		$temp = array_slice($temp,0,20);

		foreach($temp as $k => $v){
			$mids .= "'".$v['mid']."',";
		}
		$mids = substr($mids,0,-1);		

		$param['table'] = 'movie ';
		$param['field'] = ' id,title,subdesc,thumb,story,ratings ';
		$param['where'] = " id IN ($mids) ";
		//$param['order'] = 'comments DESC ';
		$param['page']= '1';		
		$param['rows'] = '20';	
		$play_movie = $db->getList($param);

		//de($play_movie);

		if(!empty($play_movie)){
			foreach($play_movie as $k=>$v){
				$mid = $v['id'];
				$play_movie[$k]['num'] = $movieid[$mid]['num'];
				$play_movie[$k]['story'] = csubstr($v['story'],50);	
			}
			$play_movie = multi_array_sort($play_movie,'num',SORT_DESC);
			foreach($play_movie as $k=>$v){
				$play_movie[$k]['k'] = $k + 1;	
			}
		}
		
		$tpl->assign(assignUser());
		$tpl->assign("play_movie", $play_movie);
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();

	}


}

?>
